﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BusinessLogicLayer;
using BusinessLogicLayer.Validation;
using DataAccessLayer;
using Entities;
using System.Linq.Expressions;

namespace BusinessLogicLayer
{
    public class ChiTietKeHoachGiangDayManager : IValidation<ChiTietKeHoachGiangDay>
    {
        private IRepository<ChiTietKeHoachGiangDay> _repository;

        public ChiTietKeHoachGiangDayManager(IRepository<ChiTietKeHoachGiangDay> rp)
        {
            _repository = rp;
        }

        public ValidationResult Validate(ChiTietKeHoachGiangDay obj)
        {
            var result = new ValidationResult();
            if (string.IsNullOrEmpty(obj.MaKH.Trim()))
                result.Messages.Add("Mã kế hoạch phải khác rỗng");
            return result;
        }

        public ActionResult InsertOrUpdate(ChiTietKeHoachGiangDay ct)
        {
            var result = new ActionResult();
            try
            {
                if (!_repository.InsertOrUpdate(ct))
                    result.ex.Add(new Exception("Thêm kế hoạch giảng dạy thất bại."));
            }
            catch (Exception ex)
            {

                result.ex.Add(ex);
            }
            return result;
        }

        public ActionResult Delete(ChiTietKeHoachGiangDay ct)
        {
            var result = new ActionResult();
            try
            {
                if (!_repository.Delete(ct))
                    result.ex.Add(new Exception("Xóa kế hoạch giảng dạy thất bại."));
            }
            catch (Exception ex)
            {

                result.ex.Add(ex);
            }
            return result;
        }

        public ActionResult Delete(string ct)
        {
            var result = new ActionResult();
            try
            {
                if (!_repository.Delete(s => s.MaMH == ct))
                    result.ex.Add(new Exception("Xóa kế hoạch giảng dạy thất bại."));
            }
            catch (Exception ex)
            {
                result.ex.Add(ex);
            }
            return result;
        }

        public ActionResultAsList<ChiTietKeHoachGiangDay> Find(Expression<Func<ChiTietKeHoachGiangDay, bool>> ct)
        {
            var result = new ActionResultAsList<ChiTietKeHoachGiangDay>();
            try
            {
                result.Result = _repository.Find(ct).ToList();
                if (result.Result.Count == 0)
                    result.ex.Add(new Exception("Không tìm thấy kế hoạch giảng dạy."));
            }
            catch (Exception ex)
            {
                
                result.ex.Add(ex);
            }
            return result;
        }
    }
}
